import CONFIG from './config.js'
const needLoginApi = CONFIG.needLoginApi
const API = {};
API.getJson = (route, params={}) => {

	// let url = CONFIG.host + CONFIG.url[route];
	let url = CONFIG.host + route;
	let token = uni.getStorageSync('token') || ''
	let user_id = uni.getStorageSync('user_id') || ''
	
	params.com_id = 888;
	/* params.user_id = '1'
	params.token='3715a4bdec' */
	params.user_id = user_id
	params.token=token
	params.position = 3
	return new Promise((resolve, reject) => {
		if(needLoginApi.indexOf(route) != -1 && !token){
			setTimeout(()=>{
				uni.navigateTo({
					url:'/pages/login/login'
				})
			},1000)
			return resolve({code:0,message:'请先登录'});
		}
		uni.request({
			url: url,
			data: params,
			dataType:"json",
			method: "GET",
			success(res) {
				// console.log(res)
				if (res.statusCode != 200) {
					uni.showToast({
						title: '网络错误',
						duration: 2000
					});
					return false;
				}
				if(res.data.code == -1){
					uni.clearStorage()
				}
				resolve(res.data)
			},
			fail(res) {
				console.log(`request调用失败`);
			}
		});
	});
}
API.getUpload = (route, files,params={},name) => {
	let url = CONFIG.host + route;
	let token = uni.getStorageSync('token') || ''
	let user_id = uni.getStorageSync('user_id') || ''
	params.com_id = 888;
	params.user_id = user_id
	params.token=token
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: url,
			filePath: files,
			formData:params,
			name: name,
			success: (res) => {
				console.log(res.statusCode)
				if (res.statusCode != 200) {
					uni.showToast({
						title: '网络错误',
						duration: 2000
					});
					return false;
				}
				resolve(JSON.parse(res.data))
			}
		});
	});
}
//获取微信支付参数
API.getPayParams = (params)=>{
	
	uni.showLoading({
	    title: '加载中',
		mask:true
	});
	let url = CONFIG.host+ CONFIG.url['orders_wxPay'];
	let token = uni.getStorageSync('token') || ''
	let user_id = uni.getStorageSync('user_id') || ''
	
	params.com_id = 888;
	params.user_id = user_id
	params.token=token
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			data: params,
			dataType:"json",
			method: "GET",
			success(res) {
				uni.hideLoading();
				// console.log(res)
				if (res.statusCode != 200) {
					uni.showToast({
						title: '网络错误',
						duration: 2000
					});
					return false;
				}
				resolve(res.data)
			},
			fail(res) {
				uni.hideLoading();
				console.log(`request调用失败`);
			}
		});
	});
}

//微信支付
API.wx_pay = function(data) {
	return new Promise((resolve, reject) => {
		uni.requestPayment({
		    provider: 'wxpay',
		    timeStamp: String(data.timeStamp),
		    nonceStr: data.nonceStr,
		    package: data.package,
		    signType: data.signType,
		    paySign: data.paySign,
		    success: function (res) {
				resolve(res);
		        console.log('success:' + JSON.stringify(res));
		    },
		    fail: function (err) {
				resolve(err);
		        console.log('fail:' + JSON.stringify(err));
		    }
		});
		
	})
}


export default API;
